//
//  TOAnimatedView.m
//  B段项目
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "TOAnimatedView.h"
#import <QuartzCore/QuartzCore.h>

@interface TOAnimatedView ()

@property(nonatomic,assign)CGPoint selfCenter;
@property(nonatomic,strong)STKAudioPlayer *player;
@property(nonatomic,strong)UIPanGestureRecognizer *panPress;
@property(nonatomic,strong)UIButton *circleBtn;
@property(nonatomic,strong)UIButton *shareBtn;
@property(nonatomic,strong)UIButton *repeatBtn;

@end

@implementation TOAnimatedView

+(TOAnimatedView *)shareAnimatedView
{
    static TOAnimatedView *myAnimateView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        myAnimateView = [[TOAnimatedView alloc] init];
        [myAnimateView createSubviews];
        
    });
    
    return myAnimateView;
}


-(void)createSubviews
{
    
    self.player = [[STKAudioPlayer alloc]initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} } ];
    
    self.backgroundColor = [UIColor clearColor];
    self.frame = CGRectMake(300, 530, 50, 50);
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 25;
    self.userInteractionEnabled = YES;
    
    
    self.clientButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.clientButton.backgroundColor = [UIColor lightGrayColor];
    self.clientButton.alpha = 0.7;
    [self.clientButton setTitle:@"静" forState:UIControlStateNormal];
    self.clientButton.titleLabel.font = [UIFont systemFontOfSize:25];
    self.clientButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.clientButton.layer.borderWidth = 2;
    self.clientButton.frame = CGRectMake(0, 0, 50, 50);
    self.clientButton.layer.masksToBounds = YES;
    self.clientButton.layer.cornerRadius = 25;
    [self.clientButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    

    self.clientButton.center = CGPointMake(25, 25);
    [self addSubview:self.clientButton];
    self.clientButton.selected = NO;
    
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.backgroundColor = [UIColor clearColor];
    self.timeLabel.textColor = [UIColor whiteColor];
    [self addSubview:self.timeLabel];
    
    
    
    
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.textColor = [UIColor whiteColor];
    [self addSubview:self.titleLabel];
   
    
    self.authorLabel = [[UILabel alloc] init];
    self.authorLabel.backgroundColor = [UIColor clearColor];
    self.authorLabel.font = [UIFont systemFontOfSize:12];
    self.authorLabel.textAlignment = NSTextAlignmentLeft;
    self.authorLabel.textColor = [UIColor whiteColor];
    [self addSubview:self.authorLabel];
 
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.backgroundColor = [UIColor clearColor];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.font = [UIFont systemFontOfSize:15];
    self.contentLabel.textColor = [UIColor whiteColor];
    [self addSubview:self.contentLabel];
  
    
    self.circleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.circleBtn.backgroundColor = [UIColor lightGrayColor];
    [self.circleBtn setTitle:@"循环" forState:UIControlStateNormal];
    [self.circleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.circleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.circleBtn.alpha = 0;
    [self addSubview:self.circleBtn];
 
    
    self.circleBtn.layer.masksToBounds = YES;
    self.circleBtn.layer.cornerRadius = 20;
    self.circleBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.circleBtn.layer.borderWidth = 2;
    [self.circleBtn addTarget:self action:@selector(circleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.circleBtn.selected = NO;
    
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareBtn.backgroundColor = [UIColor lightGrayColor];
    [self.shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    [self.shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.shareBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.shareBtn.alpha = 0;
    [self addSubview:self.shareBtn];
   
    
    self.shareBtn.layer.masksToBounds = YES;
    self.shareBtn.layer.cornerRadius = self.circleBtn.layer.cornerRadius;
    self.shareBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.shareBtn.layer.borderWidth = 2;
    [self.shareBtn addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.repeatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.repeatBtn.backgroundColor = [UIColor lightGrayColor];
    self.repeatBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.repeatBtn setTitle:@"还想再静一静" forState:UIControlStateNormal];
    self.repeatBtn.alpha = 0;
    [self addSubview:self.repeatBtn];
  
    self.repeatBtn.layer.masksToBounds = YES;
    self.repeatBtn.layer.cornerRadius = 10;
    self.repeatBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.repeatBtn.layer.borderWidth = 2;
    [self.repeatBtn addTarget:self action:@selector(repeatBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.panPress = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changeFrame:)];
    [self addGestureRecognizer:self.panPress];
    self.selfCenter = CGPointMake(326, 555);
}
#pragma mark - 
-(void)contrain
{
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(WIDTH*0.3);
        make.top.mas_equalTo(HEIGHT/5*3);
        make.height.mas_equalTo(50);
    }];
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 0.7;
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.timeLabel.mas_right).offset(10);
        make.top.height.mas_equalTo(self.timeLabel);
        make.width.mas_equalTo(5);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(view.mas_right).offset(5);
        make.width.mas_equalTo(150);
        make.top.mas_equalTo(self.timeLabel.mas_top);
        make.height.mas_equalTo(self.timeLabel).multipliedBy(0.6);
    }];
    
    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(self.timeLabel).multipliedBy(0.4);
    }];
    
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.timeLabel);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(20);
        make.right.mas_equalTo(-30);
        
    }];
    
    [self.circleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.timeLabel);
        make.centerY.mas_equalTo(self.clientButton);
        make.height.width.mas_equalTo(40);
    }];
    
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.circleBtn.mas_right).offset(20);
        make.centerY.mas_equalTo(self.clientButton);
        make.height.width.mas_equalTo(self.circleBtn);
    }];
    
    [self.repeatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.shareBtn.mas_right).offset(20);
        make.right.mas_equalTo(self.clientButton.mas_left).offset(-20);
        make.centerY.height.mas_equalTo(self.circleBtn);
    }];
}

#pragma mark - 移除约束
-(void)removeContrain
{
    [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
    }];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
    }];
    [self.authorLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
    }];
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
    }];
    [self.circleBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
    }];
    [self.shareBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
    }];
    [self.repeatBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
    }];
    
}
#pragma mark - 赋值方法
-(void)setJingDic:(NSMutableDictionary *)jingDic
{
    _jingDic = jingDic;
    [self sd_setImageWithURL:[jingDic objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@""]];
    self.musicUrl = [jingDic objectForKey:@"audio"];
    self.titleLabel.text = [jingDic objectForKey:@"title"];
    self.contentLabel.text = [jingDic objectForKey:@"text"];
    self.authorLabel.text = [jingDic objectForKey:@"author"];
}
#pragma mark - 还想静静点击方法
-(void)repeatBtnAction:(UIButton *)btn
{
    [self.player playURL:[NSURL URLWithString:_musicUrl]];
    [UIView animateWithDuration:0.5 animations:^{
        
        btn.alpha = 0;
    }];
}
#pragma mark - 分享按钮点击方法
-(void)shareBtnAction:(UIButton *)btn
{
   
    [self.animatedViewDelegate presentShareViewWithContent:[self.jingDic objectForKey:@"shareinfo"]];
}
#pragma mark - 循环按钮点击方法
-(void)circleBtnAction:(UIButton *)btn
{
    if (!btn.selected) {
        
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor orangeColor].CGColor;
    }
    else
    {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    }
    btn.selected = !btn.selected;

}

#pragma mark - 按钮点击方法
-(void)btnAction:(UIButton *)btn
{
    if (!btn.selected) {
        
        [self changeViewFullScreen];
        self.clientButton.center = CGPointMake(326, 555);
        [self.clientButton setTitle:@"×" forState:UIControlStateNormal];
        [self removeGestureRecognizer:self.panPress];
        [[ZZCMusicPlayerViewController shareMusicPlayerViewController].player pause];
    }
    else
    {
        [self changeViewBottomRight];
        self.circleBtn.alpha = 0;
        self.shareBtn.alpha = 0;
        [self.clientButton setTitle:@"静" forState:UIControlStateNormal];
        [self addGestureRecognizer:self.panPress];
    }
    
    btn.selected = !btn.selected;
}

#pragma mark - 拖动方法
-(void)changeFrame:(UIPanGestureRecognizer *)panPress
{
    CGPoint point = [panPress translationInView:self];
    self.center = CGPointMake(self.centerX+point.x, self.centerY+point.y);
    self.selfCenter = self.center;
    [panPress setTranslation:CGPointMake(0, 0) inView:self];
}

#pragma mark - view变大
-(void)changeViewFullScreen
{

    [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{

        [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.4 animations:^{
            self.circleBtn.alpha = 0.7;
            self.shareBtn.alpha = 0.7;
            self.frame = CGRectMake(0, 0, WIDTH, HEIGHT-64);

        }];
        
    } completion:^(BOOL finished) {
        

        [self contrain];


    }];
    [self.player playURL:[NSURL URLWithString:_musicUrl]];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(track) userInfo:nil repeats:YES];
    
}

#pragma mark - 定时器方法
-(void)track
{
    self.timeLabel.text = [NSString stringWithFormat:@"%.0fs''", self.player.duration - self.player.progress];
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont systemFontOfSize:40];
    
    if (self.player.progress > 59) {
        
        if (self.circleBtn.selected) {
            [self.player playURL:[NSURL URLWithString:_musicUrl]];
        }else{
            self.repeatBtn.alpha = 0.7;
        }
    }
    
}


#pragma mark - view变小
-(void)changeViewBottomRight
{
    [self removeContrain];
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
        
        [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.4 animations:^{
            
            self.clientButton.center = CGPointMake(25, 25);

            self.frame = CGRectMake(300, 530, 50, 50);
            self.center = self.selfCenter;
        }];
        
    } completion:^(BOOL finished) {
        
        
    }];
    
    [self.player stop];
    [self.timer invalidate];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
